package com.zhiqianbao.utils;

import android.content.res.Resources;

import com.zhiqianbao.R;
import com.zhiqianbao.entity.WorkIndustryInfo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by hp on 2017/11/2.
 */
public class WorkIndustryDataUtils {
    /**
     * 主行业分类，行业一级分类
     */
    private ArrayList<WorkIndustryInfo> options1Items = new ArrayList<>();
    /**
     * 次行业分类，行业二级分类
     */
    private ArrayList<ArrayList<WorkIndustryInfo>> options2Items = new ArrayList<>();
    /**
     * 二级行业通过code值快速查询对应分类名称
     */
    private Map<Integer,String> workMap=new HashMap<>();

    /**
     * 一级行业分类编码code
     */
    private int oneTypeIndex=0;
    /**
     * 二级行业分类编码code
     */
    private int twoTypeIndex=0;
    public WorkIndustryDataUtils(Resources resources){
        initData(resources);
    }

    public void initData(Resources resources){
        String[] array_work_one_type = resources
                .getStringArray(R.array.work_industry_first_type);
        int[] array_work_one_code=resources
                .getIntArray(R.array.work_industry_first_code);
        String[] array_work_second_type = resources
                .getStringArray(R.array.work_industry_second_type);
        int[] array_work_second_code=resources
                .getIntArray(R.array.work_industry_second_code);
        int firstSizes=array_work_one_code.length;
        int secondSizes=array_work_second_code.length;
        ArrayList<WorkIndustryInfo> secondItems = new ArrayList<>();
        for(int i=0;i<secondSizes;i++){
            WorkIndustryInfo industryInfo=new WorkIndustryInfo();
            industryInfo.setCode(array_work_second_code[i]);
            industryInfo.setWorkIndustry(array_work_second_type[i]);
            secondItems.add(industryInfo);
            workMap.put(array_work_second_code[i],array_work_second_type[i]);
        }
        for(int i=0;i<firstSizes;i++){
            WorkIndustryInfo industryInfo=new WorkIndustryInfo();
            industryInfo.setCode(array_work_one_code[i]);
            industryInfo.setWorkIndustry(array_work_one_type[i]);
            options1Items.add(industryInfo);
            ArrayList<WorkIndustryInfo> items=new ArrayList<>();
            switch (i+1){
                case 1:
                    items= new ArrayList<WorkIndustryInfo>(secondItems.subList(0,6));
                    break;
                case 2:
                    items= new ArrayList<WorkIndustryInfo>(secondItems.subList(6,11));
                    break;
                case 3:
                    items=new ArrayList<WorkIndustryInfo>(secondItems.subList(11,12));
                    break;
                case 4:
                    items= new ArrayList<WorkIndustryInfo>(secondItems.subList(12,20));
                    break;
                case 5:
                    items= new ArrayList<WorkIndustryInfo>(secondItems.subList(20,24));
                    break;
                case 6:
                    items=new ArrayList<WorkIndustryInfo>(secondItems.subList(24,32));
                    break;
            }
            options2Items.add(items);
        }
    }

    /**
     * 获取工作信息
     * @param profession
     * @return
     */
    public String getWorkMsg(String profession){
        String[] workCodes=profession.split("-");
        if(workCodes.length!=2){
            return "";
        }
        else{
            if(isNumeric(workCodes[0]) && isNumeric(workCodes[1])){
                int code1=Integer.parseInt(workCodes[0]);
                int code2=Integer.parseInt(workCodes[1]);
                if(code1==0||code2==0){
                    return "";
                }
                else{
                    oneTypeIndex=code1;
                    twoTypeIndex=code2;
                    return getFirstWorkMsg(code1>6 ? 5 :(code1-1))
                            +"—"+getSecondtWorkMsg(code2);
                }
            }
            else{
                return "";
            }
        }
    }
    /**
     * 获取行业一级分类名称
     * @param position
     * @return
     */
    public String getFirstWorkMsg(int position){
        if((options1Items.size()-1)<position){
            return "";
        }
        return options1Items.get(position).getWorkIndustry();
    }

    /**
     * 获取行业二级分类名称
     * @param code
     * @return
     */
    public String getSecondtWorkMsg(int code){
        if(workMap.get(code)!=null) {
            return workMap.get(code);
        }
        else{
            return "";
        }
    }
    public int getOneTypeIndex() {
        return oneTypeIndex;
    }

    public int getTwoTypeIndex() {
        return twoTypeIndex;
    }
    public ArrayList<WorkIndustryInfo> getOptions1Items() {
        return options1Items;
    }

    public void setOptions1Items(ArrayList<WorkIndustryInfo> options1Items) {
        this.options1Items = options1Items;
    }

    public ArrayList<ArrayList<WorkIndustryInfo>> getOptions2Items() {
        return options2Items;
    }

    public void setOptions2Items(ArrayList<ArrayList<WorkIndustryInfo>> options2Items) {
        this.options2Items = options2Items;
    }

    public Map<Integer, String> getWorkMap() {
        return workMap;
    }

    public void setWorkMap(Map<Integer, String> workMap) {
        this.workMap = workMap;
    }

    /**
     * 判断字符串是否是数字
     * @param str
     * @return
     */
    public boolean isNumeric(String str){
        Pattern pattern = Pattern.compile("[0-9]*");
        Matcher isNum = pattern.matcher(str);
        if( !isNum.matches() ){
            return false;
        }
        return true;
    }
}
